export default {
  createNode(x, y){
    // if(x < 0 || y < 0){
    //   this.dialogs.showWarning('错误', '节点定位不可为负数');
    //   return;
    // }

    let data = this.getCurrentSitePos();

    let isExist = data.find(item => item[0] == x && item[1] == y);
    if(isExist){
      this.dialogs.showWarning('错误', '当前节点不可使用');
      return;
    }

    if(this.mapClip){
      this.createMapNode(x, y);
    } else {
      this.createWorldNode(x, y);
    }
    this.renderView();
    this.moveGrid([x, y]);
  },

  createMapNode(x, y){
    let data = {
      key: '编辑器/空节点',
      mapPos: [x, y],
      attr: {
        next: []
      }
    }

    this.mapClip.map.push(data);
  },

  createWorldNode(x, y){
    let data = {
      worldName: '空节点',
      worldPos: [x, y],
      worldDesc: '待编辑',
      map: [
        {
          key: '编辑器/空节点',
          mapPos: [0, 0],
          attr: {
            next: []
          }
        }
      ]
    }

    this.mapData.world.push(data);
  },
  
  deleteNode(x, y){
    if(x == 0 && y == 0){
      this.dialogs.showWarning('错误', '节点[0, 0]不可删除');
      return;
    }

    let data = this.getCurrentSitePos();

    let isExist = data.find(item => item[0] == x && item[1] == y);
    if(!isExist){
      this.dialogs.showWarning('错误', '节点不存在');
      return;
    }

    if(this.mapClip){
      this.mapClip.map = this.mapClip.map.filter(item => {
        if(item.mapPos[0] == x && item.mapPos[1] == y){
          return false;
        } else {
          item.attr.next = item.attr.next.filter(item => {
            return item.pos[0] != x || item.pos[1] != y;
          })

          return true;
        }
      })
    } else {
      this.mapData.world = this.mapData.world.filter(item => {
        return item.worldPos[0] != x || item.worldPos[1] != y;
      })
    }

    this.renderView(false);
  }
}